﻿
//加载微信
; function wxShare(_title, _desc,_link, _img) {

    ajax(
        {
            //http://ftwx.fangte.com/wechat/other/getsettings
            //http://dpanpeter.imwork.net/other/getsettings
            url: 'http://ftwx.fangte.com/wechat/other/getsettings'
            , type: 'POST'
            , dataType: 'JSON'
            , data: {url:window.location.href}
            , success: function (settingsObj) {

                //引入微信JS
                var scriptObj = document.createElement('script');                
                scriptObj.type = 'text/javascript';                

                if (scriptObj.readyState) {
                    scriptObj.onreadystatechange = function () {
                        if (scriptObj.readyState == "loaded" || scriptObj.readyState == "complete") {
                            scriptObj.onreadystatechange = null;
                            wxLoadCall(settingsObj, _title, _desc, _link, _img);
                        }
                    };
                } else {
                    scriptObj.onload = function () {
                        wxLoadCall(settingsObj, _title, _desc, _link, _img);
                    };
                }

                scriptObj.src = 'http://res.wx.qq.com/open/js/jweixin-1.2.0.js';
                document.getElementsByTagName("head")[0].appendChild(scriptObj);
            }
            , error: function () {

            }
        },

    this);
};

function wxLoadCall(settingsObj, _title, _desc, _link, _img)
{
    //微信配置信息
    wx.config({
        debug: false,
        appId: settingsObj.appid,
        timestamp: settingsObj.timestamp,
        nonceStr: settingsObj.noncestr,
        signature: settingsObj.signature,
        jsApiList: ['chooseImage', 'uploadImage', 'hideMenuItems', 'onMenuShareTimeline', 'onMenuShareAppMessage']
    });

    //当验证过后，config信息验证后会执行ready方法，
    //所有接口调用都必须在config接口获得结果之后，config是一个客户端的异步操作，
    //所以如果需要在页面加载时就调用相关接口，
    //则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口，则可以直接调用，不需要放在ready函数中。
    wx.ready(function () {
        wx.hideMenuItems({
            //'menuItem:share:qq','menuItem:share:QZone',
            menuList: ['menuItem:share:qq','menuItem:share:QZone','menuItem:share:weiboApp', 'menuItem:favorite', 'menuItem:share:facebook', 'menuItem:editTag', 'menuItem:delete', 'menuItem:copyUrl', 'menuItem:originPage', 'menuItem:readMode', 'menuItem:openWithQQBrowser', 'menuItem:openWithSafari', 'menuItem:share:email', 'menuItem:share:brand']
            // 要隐藏的菜单项，只能隐藏“传播类”和“保护类”按钮，所有menu项见附录3
        });

        //分享到朋友圈事件
        wx.onMenuShareTimeline({
            title: _title, // 分享标题
            link: _link, // 分享链接
            imgUrl: _img, // 分享图标
            success: function () {

            },
            cancel: function () {
                // 用户取消分享后执行的回调函数
            },
            error: function () {
                //  alert("调用微信分享接口失败");
            }
        });

        //发送给朋友
        wx.onMenuShareAppMessage({
            title: _title, // 分享标题
            desc: _desc, // 分享描述
            link: _link, // 分享链接
            imgUrl: _img, // 分享图标
            type: '', // 分享类型,music、video或link，不填默认为link
            dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
            success: function () {
            },
            cancel: function () {
                // 用户取消分享后执行的回调函数
            }
        });

    });


    wx.error(function (res) {
        //如果config信息验证失败会执行error函数
        //alert(location.href.split('#')[0])
    });

}


//生成XMLHttp请求对象
; function getXMLHttpObj() {
    var o = null;

    if (window.XMLHttpRequest) {
        o = new window.XMLHttpRequest;
    }
    else {
        o = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return o;
};

//ajax请求
function ajax(opts, that) {

    var o = getXMLHttpObj();
    var paras = '';
    if (opts.data) {
        for (var s in opts.data) {
            if (paras != '') {
                paras+='&'
            }
            paras += s + '=' + opts.data[s];
        }
    }

    switch (opts.type.toUpperCase()) {
        case 'GET':
            o.open(opts.type, opts.url + '?' + paras, opts.async);
            break;
        case 'POST':
            o.open(opts.type, opts.url, opts.async);
            o.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            break;
        case 'PUT': break;
        case 'DELETE': break;
        default: break;
    }
    
    o.send(paras);

    if (opts.async) {
        o.onreadystatechange = function () {
            if (o.readyState == 4 && o.status == 200) {
                if (opts.success && typeof (opts.success) == 'function') {

                    switch (opts.dataType.toUpperCase()) {
                        case 'JSON':

                            opts.success.call(that, eval('(' + o.responseText + ')'));

                            break;
                        default: break;
                    }

                }
            }
            else {

                if (opts.error && typeof (opts.error) == 'function') //发生错误时
                {
                    opts.error.call(that, o, o.statusText, null);
                }
            }
        }
    }
    else {
        if (opts.success && typeof (opts.success) == 'function') {
            switch (opts.dataType.toUpperCase()) {
                case 'JSON':
                    opts.success.call(that, eval('(' + o.responseText + ')'));
                    break;
                default: break;
            }
        }
    }
}
